package 
{
	import flash.display.MovieClip;
	
	import fl.controls.List;
	import fl.data.DataProvider;
	
	import rosa.RosaSettings;
	import rosa.events.RosaEvent;
	import rosa.services.ServiceError;
	import rosa.services.ServiceProxy;		

	public class ServiceConsumer extends MovieClip
	{
		private var service:ServiceProxy;
		
		public var lista:List;

		public function ServiceConsumer()
		{
			// First setup the settings. This is done once in all the application
			with(RosaSettings)
			{
				// The url used when testing in Flash
				localTestGatewayURL = "http://192.168.0.7/gateway.aspx";
				// The url used when debugging
				debugGatewayURL = "http://192.168.0.7/gateway.aspx";
				// The url used in production
				gatewayURL = "gateway.aspx";
				// credentials, if they are not needed delete this lines
				username = "anonymous";
				password = "anonymous";
			}
			
			// creates the typed service
			service = new ServiceProxy("MarioSierra.Core.Services.GaleriasService");
			// Event for success calls
			service.addEventListener("ObtenerCategoriasResult", getMasterDataResult);
			// Event for fault calls			service.addEventListener("ObtenerCategoriasFault", getMasterDataFault);
			// Event for the calls starts			service.addEventListener("ObtenerCategoriasCall", getMasterDataCall);
			
			// Call the method
			service.ObtenerCategorias();
		}
		
		private function getMasterDataCall(event:RosaEvent):void
		{
			// Show some preloading message
			trace("calling the server...");
		}

		private function getMasterDataFault(event:RosaEvent):void
		{
			// Show some error message
			trace("Error ocurred: "+event.error.description);
		}

		private function getMasterDataResult(event:RosaEvent):void
		{
			// Do something with the data
			trace("Got :"+event.result);
			lista.dataProvider = new DataProvider(event.result);
			lista.labelField = "Nombre";
		}
	}
}
